<?php
/**
 * 用户许可（权限）
 * (model)
 */
namespace App\Util;

class User
{
    /**
     * 获取用户email
     * @param $id
     * @return mixed
     */
    public static function getEmailById($id){
        $userModel = config('admin.database.users_model');
        return $userModel::query()->where('id',$id)->value('email');
    }

    public static function getIdByUsername($username){
        $userModel = config('admin.database.users_model');
        return $userModel::query()->where('username',$username)->value('id');
    }

    public static function getUsernameById($id){
        $userModel = config('admin.database.users_model');
        return $userModel::query()->where('id',$id)->value('username');
    }

    /**
     * 所有用户
     * @param bool $inResign
     * @return mixed
     */
    public static function getUserOptions(bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        return $query->pluck('username','id');
    }

    /**
     * 产品负责人
     * @param bool $inResign
     * @return mixed
     */
    public static function getMUserOptions(bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        return $query->get()->filter(function ($item){
            return $item->inRoles(['administrator',
                                    'product-director',
                                   'pe',
                                   'product-designer',
                                   'industrial-designer',
                                   'pm',
                                   'qe',
                                   'purchasing-manager',
                                   'purchasing-specialist']);
        })->pluck('username','id');
    }

    /**
     * 产品创建人
     * @param bool $inResign
     * @return mixed
     */
    public static function getCUserOptions(bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        return $query->get()->filter(function ($item){
            return $item->inRoles(['administrator',
                                   'product-director',
                                   'pe',
                                   'product-designer',
                                   'industrial-designer',
                                   'pm',
                                   'qe',
                                   'purchasing-manager',
                                   'purchasing-specialist']);
        })->pluck('username','id');
    }

    /**
     * 亚马逊产品销售员
     * @param bool $inResign
     * @return mixed
     */
    public static function getSellerOptions(bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        return $query->get()->filter(function ($item){
            return $item->inRoles(['administrator',
                                    'sales-director',
                                   'sales-executive',
                                   'salesperson',
                                   'customer-service']);
        })->pluck('username','id');
    }

    /**
     * 亚马逊产品创建人
     * @param bool $inResign
     * @return mixed
     */
    public static function getCSellerOptions(bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        return $query->get()->filter(function ($item){
            return $item->inRoles(['administrator',
                                   'sales-director',
                                   'sales-executive',
                                   'salesperson',
                                   'customer-service']);
        })->pluck('username','id');
    }

//    public static function options($join = []){
//        $query = self::query();
//        if($join && isset($join['table']) && isset($join['field'])){
//            $userTable = config('admin.database.users_table');
//            $joinTable = $join['table'];
//            $joinField = $join['field'];
//            $query = $query->join($joinTable,$userTable.'.id','=',$joinTable.'.'.$joinField)
//                ->select($userTable.'.id',$userTable.'.username')
//                ->distinct();
//        }
//        return $query->pluck('username','id');
//    }

    /**
     * 站点负责人
     * @param bool $inResign
     * @return mixed
     */
    public static function getAMUserOptions(bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        return $query->get()->filter(function ($item){
            return $item->inRoles(['administrator',
                                    'sales-executive',
                                    'jp-salesperson',
                                    'purchasing-specialist',
                                    'operation-specialist']);
        })->pluck('username','id');
    }

    /**
     * 亚马逊站点销售
     * @param $area
     * @param bool $inResign
     * @return mixed
     */
    public static function getASellerOptions($area, bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        $_area = strtolower($area);
        return $query->get()->filter(function ($item) use ($_area){
            return $item->inRoles([$_area.'-sales-executive', $_area.'-salesperson']);
        })->pluck('username','id');
    }

    /**
     * 亚马逊站点销售ID
     * @param $area
     * @param bool $inResign
     * @return mixed
     */
    public static function getASellerIds($area, bool $inResign = true){
        $userModel = config('admin.database.users_model');
        $query = $inResign ? $userModel::query() : $userModel::query()->where('status',1);
        $_area = strtolower($area);
        return $query->get()->filter(function ($item) use ($_area){
            return $item->inRoles([$_area.'-sales-executive', $_area.'-salesperson']);
        })->pluck('id');
    }
}
